package com.example.springbootquartz.controller;
import com.example.springbootquartz.config.MyQuartzScheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/08/05  15:23
 * @Description:
 */

@RestController
@RequestMapping("/quartz")
public class QuartzController {
    @Autowired
    private MyQuartzScheduler myQuartzScheduler;
    /**
     * 开始执行所有任务
     */
    @RequestMapping("/startAllJobs")
    public void startAllJobs() {
        try {
            myQuartzScheduler.startAllJobs();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取job信息
     */
    @RequestMapping("/getJobInfo")
    public String getJobInfo(String name, String group) {
        String info = null;
        try {
            info = myQuartzScheduler.getJobInfo(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return info;
    }
    /**
     * 修改某个job任务的执行时间
     */
    @RequestMapping("/modifyJobTime")
    public boolean modifyJobTime(String name, String group, String time) {
        boolean flag = true;
        try {
            flag = myQuartzScheduler.modifyJobTime(name, group, time);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return flag;
    }
    /**
     * 暂停某个job定时任务
     */
    @RequestMapping(value = "/pauserJob")
    public void pauserJob(String name, String group) {
        try {
            myQuartzScheduler.pauserJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    /**
     * 暂停所有job定时任务
     */
    @RequestMapping(value = "/pauserAllJobs")
    public void pauserAllJobs() {
        try {
            myQuartzScheduler.pauserAllJobs();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    /**
     * 删除某个job定时任务
     */
    @RequestMapping(value = "/deleteJob")
    public void deleteJob(String name, String group) {
        try {
            myQuartzScheduler.deleteJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

}
